package com.coustemview.qianfeng.huaqiangbeistore.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.fragment.Fragment_mainpage;
import com.coustemview.qianfeng.huaqiangbeistore.fragment.Fragment_my;
import com.coustemview.qianfeng.huaqiangbeistore.fragment.Fragment_shopcar;

public class MainActivity extends AppCompatActivity {
   private RadioGroup mRadioGroup;
    private Fragment mCurrentFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setListener();
    }

    private void initData() {
        manager = getSupportFragmentManager();
        //初始化
        showAndHide(R.id.framelayout, Fragment_mainpage.class);
    }

    private void setListener() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Class<? extends Fragment> clazz = null;
                switch (checkedId) {
                    case R.id.radio_mainpage :
                clazz= Fragment_mainpage.class;
                        break;
                    case R.id.radio_shopcar :
                clazz= Fragment_shopcar.class;
                        break;
                    case R.id.radio_my :
                  clazz= Fragment_my.class;
                        break;
                    default:
                        break;
                }
                showAndHide(R.id.framelayout, clazz);
            }
        });
    }

    private void initView() {
        mRadioGroup= (RadioGroup) findViewById(R.id.radiogroup);
    }
    private void showAndHide(int contentId, Class<? extends Fragment> clazz) {
        // 判断当前的fragment和需要替换掉的fragment是不是同一个
        if (mCurrentFragment != null && mCurrentFragment.getClass().getSimpleName().equals(clazz.getSimpleName())) {
            return;
        }


        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fragment = null;
        try {
            //判断fragment有没有添加过
            Fragment fragmentByTag = manager.findFragmentByTag(clazz.getSimpleName());
            if (fragmentByTag != null) {
                // 显示需要 显示的fragment
                transaction.show(fragmentByTag);
                // 隐藏当前的fragment
                transaction.hide(mCurrentFragment);
                // 记录当前显示的fragment
                mCurrentFragment = fragmentByTag;
            } else {
                // 通过无参的 公开的构造函数来创建fragment实例
                fragment = clazz.newInstance();
                // 当前的fragment没有没有添加过
                // 把fragment添加到fragmentManager 里面
                transaction.add(contentId, fragment, clazz.getSimpleName());
                // 隐藏当前的fragment
                if (mCurrentFragment != null) {
                    transaction.hide(mCurrentFragment);
                }
                // 记录当前的fragment是哪个
                mCurrentFragment = fragment;
            }
            transaction.commit();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
